/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package restAPI;

import internalAPI.QueryExecutor;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.restlet.Restlet;
import org.restlet.Server;
import org.restlet.data.Protocol;
import org.restlet.routing.Router;
import org.restlet.routing.Template;
import statlar.Statlar;

/**
 *
 * @author ferco
 * 
 * Main class for REST API, starts the server and set up the router
 */
public class RestAPI extends Restlet{
    
    //Router used for routing requests based on URIs
    Router router;
    
    //Executor for working with database
    static QueryExecutor executor;  
    
    
    /**
     *
     * @author ferco
     * 
     * @param address IP address of server
     * @param executor instance of QuerzExecutor used for comunication with database
     */
    public void configureServer(String address, QueryExecutor executor){
        
        
        router = new Router(this.getContext());  
        
        router.setDefaultMatchingMode(Template.MODE_STARTS_WITH);
        router.setRoutingMode(Router.MODE_LAST_MATCH);
        
        router.setDefaultMatchingQuery(true);
        
        
        // setting up routing URIs
        router.attach(address + "/place/{place}",PlaceResource.class);
        router.attach(address + "/places/",PlacesResource.class);  
        router.attach(address + "/caves/",CavesResource.class);
        router.attach(address + "/basements/",BasementsResource.class);  
        router.attach(address + "/abysses/",AbyssesResource.class); 
        router.attach(address + "/sewers/",SewersResource.class); 


        this.executor = executor;

    
    }
    /**
     *
     * @author ferco
     * 
     * @param port, number of port that uses our aplication
     */
    public void startServer(int port){
        try {
            new Server(Protocol.HTTP, port, router).start();
        } catch (Exception ex) {
            System.err.println("Unable to start server! " + ex.getMessage());
        }
    }
    
}
